package org.openea.eap.module.obpm.service.form;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.openbpm.base.core.id.IdUtil;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.core.util.XmlCovertUtil;
import org.openbpm.bus.manager.BusinessObjectManager;
import org.openbpm.bus.model.xml.BusinessObjectXml;
import org.openea.eap.module.obpm.model.form.FormDef;
import org.openea.eap.module.obpm.model.form.xml.FormXml;
import org.openea.eap.module.obpm.model.form.xml.FormXmlList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FormImExportService {

    @Resource
    FormDefManager formDefManager;

    @Resource
    BusinessObjectManager businessObjectManager;

    public Map<String, String> exportFormXmlList(String... objIds) throws Exception {
        Map<String, String> exportFiles = new HashMap<String, String>();
        FormXmlList formList = new FormXmlList();
        for (String id : objIds) {
            FormXml formXml = getFormXml(id);
            if (formXml != null) {
                formList.getFormList().add(formXml);
            }
        }
        String xml = XmlCovertUtil.covert2Xml(formList);
        exportFiles.put("form.xml", xml);
        return exportFiles;
    }

    public FormXml getFormXml(String formId) {
        FormDef formDef = formDefManager.get(formId);
        if (formDef == null) {
            return null;
        }

        FormXml formXml = new FormXml(formDef);
        String boKey = formDef.getBoKey();
        if (StringUtil.isNotEmpty(boKey)) {
            BusinessObjectXml boXml = businessObjectManager.getBOXmlByKey(boKey);
            if (boXml != null) {
                formXml.setBusinessObject(boXml);
            }
        }
        return formXml;
    }

    public List<Map<String, Object>> importFormPreview(String strFormXml) throws Exception {
        strFormXml = checkFormXmlFormat(strFormXml);

        List<Map<String, Object>> resultList = new ArrayList();
        FormXmlList formXmlList = XmlCovertUtil.covert2Object(strFormXml, new Class[]{FormXmlList.class});
        for (FormXml formXml : formXmlList.getFormList()) {
            Map<String, Object> map = new HashMap();
            map.put("formDef", formXml);
            // 表单 / 业务对象
            map.put("formName", formXml.getName());
            map.put("formKey", formXml.getKey());
            map.put("boName", formXml.getBoName());
            map.put("boKey", formXml.getBoKey());
            //  包含实体表 / 相关表单 / 相关流程
            BusinessObjectXml boXml = formXml.getBusinessObject();
            if (boXml != null) {
                Map<String, Object> mOverview = businessObjectManager.getOverviewFromBOXml(boXml);
                map.putAll(mOverview);
            }
            resultList.add(map);
        }
        return resultList;
    }

    private String checkFormXmlFormat(String xml) throws Exception {
        //检查xml文件格式是否正确
        if (xml != null && xml.indexOf("fromXmlList>") > 0) {
            xml = xml.replaceAll("fromXmlList>", "formXmlList>");
        }
        Document doc = DocumentHelper.parseText(xml);
        Element root = doc.getRootElement();
        String msg = "导入文件格式不对";
        if (!root.getName().equals("formXmlList")) {
            throw new Exception(msg);
        }
        List<Element> itemLists = root.elements();
        for (Element elm : itemLists) {
            if (!elm.getName().equals("formXml")) {
                throw new Exception(msg);
            }
        }
        return xml;
    }

    public void importFormSave(List<JSONObject> overViewList) {
        for (JSONObject json : overViewList) {
            // 1. 解析
            JSONObject formJson = json.getJSONObject("formDef");
            FormXml formXml = JSON.toJavaObject(formJson, FormXml.class);
            // 2. 保存formXml
            importFormSave(formXml);
            // 3. 保存businessObjectXml
            BusinessObjectXml boXml = formXml.getBusinessObject();
            businessObjectManager.importBusinessObjectSave(boXml);
        }
    }

    public void importFormSave(FormXml formXml) {
        FormDef formDef = formDefManager.getByKey(formXml.getKey());
        if (formDef == null) {
            formDef = new FormDef();
        }
        BeanUtil.copyProperties(formXml, formDef);
        if (formDef.getId() == null) {
            formDef.setId(IdUtil.getSuid());
            formDefManager.create(formDef);
        } else {
            formDefManager.update(formDef);
        }
    }
}
